package com.ruoyi.factory.costControl.controller;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factory.common.utils.IdGen;
import com.ruoyi.factory.costControl.service.CostHandleService;
import com.ruoyi.factory.tododemo.message.crypto.EncryptionHolder;
import com.ruoyi.factory.tododemo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO  胜意商旅费控有关接口对接
 */
@EnableScheduling
@RestController("costHandleController")
@RequestMapping("/api/coatControHandle")
public class CostHandleController extends BaseController {
    // 保存时间校验
    private Map<String,String> verify = new HashMap<>();
    // 请求间隔时间 毫秒
    private long ins = 10000;

    @Autowired
    private CostHandleService costHandleService;

    @Resource
    private DemoService service;

    /**
     * 获取员工信息
     * @return
     */
    @PostMapping("/getStaffInfo")
    public AjaxResult getStaffInfo(@RequestBody String req, HttpServletRequest request){
        String getStaffInfo = verify.get("insecond");
        long l = System.currentTimeMillis();
        if(StringUtils.isNotEmpty(getStaffInfo)){
            Long aLong = Long.valueOf(getStaffInfo);
            long result=l-aLong;
            if(result < ins){
                verify.put("insecond",String.valueOf(l));
                return new AjaxResult(HttpStatus.ERROR,"error","请勿频繁请求");
            }else{
                verify.put("insecond",String.valueOf(l));
            }
        }else{
            verify.put("insecond",String.valueOf(l));
        }
        return costHandleService.getStaffInfo(req, request);
    }

    /**
     * 获取个人银行卡信息
     * @param req
     * @param request
     * @return
     */
    @PostMapping("/getPersonBankInfo")
    public AjaxResult getPersonBankInfo(@RequestBody String req, HttpServletRequest request){
        String getStaffInfo = verify.get("insecond");
        long l = System.currentTimeMillis();
        if(StringUtils.isNotEmpty(getStaffInfo)){
            Long aLong = Long.valueOf(getStaffInfo);
            long result=l-aLong;
            if(result < ins){
                verify.put("insecond",String.valueOf(l));
                return new AjaxResult(HttpStatus.ERROR,"error","请勿频繁请求");
            }else{
                verify.put("insecond",String.valueOf(l));
            }
        }else{
            verify.put("insecond",String.valueOf(l));
        }
        return costHandleService.getPersonBankInfo(req, request);
    }

    /**
     * 获取客户信息
     * @param req
     * @param request
     * @return
     */
    @PostMapping("/getCustomerInfo")
    public AjaxResult getCustomerInfo(@RequestBody String req, HttpServletRequest request){
        String getStaffInfo = verify.get("insecond");
        long l = System.currentTimeMillis();
        if(StringUtils.isNotEmpty(getStaffInfo)){
            Long aLong = Long.valueOf(getStaffInfo);
            long result=l-aLong;
            if(result < ins){
                verify.put("insecond",String.valueOf(l));
                return new AjaxResult(HttpStatus.ERROR,"error","请勿频繁请求");
            }else{
                verify.put("insecond",String.valueOf(l));
            }
        }else{
            verify.put("insecond",String.valueOf(l));
        }
        return costHandleService.getCustomerInfo(req, request);
    }

    /**
     * 获取项目信息
     * @param req
     * @param request
     * @return
     */
    @PostMapping("/getProjectInfo")
    public AjaxResult getProjectInfo(@RequestBody String req, HttpServletRequest request){
        String getStaffInfo = verify.get("insecond");
        long l = System.currentTimeMillis();
        if(StringUtils.isNotEmpty(getStaffInfo)){
            Long aLong = Long.valueOf(getStaffInfo);
            long result=l-aLong;
            if(result < ins){
                verify.put("insecond",String.valueOf(l));
                return new AjaxResult(HttpStatus.ERROR,"error","请勿频繁请求");
            }else{
                verify.put("insecond",String.valueOf(l));
            }
        }else{
            verify.put("insecond",String.valueOf(l));
        }
        return costHandleService.getProjectInfo(req, request);
    }

    /**
     * 获取资金项目信息
     * @param req
     * @param request
     * @return
     */
    @PostMapping("/getFundplanInfo")
    public AjaxResult getFundplanInfo(@RequestBody String req, HttpServletRequest request){
        String getStaffInfo = verify.get("insecond");
        long l = System.currentTimeMillis();
        if(StringUtils.isNotEmpty(getStaffInfo)){
            Long aLong = Long.valueOf(getStaffInfo);
            long result=l-aLong;
            if(result < ins){
                verify.put("insecond",String.valueOf(l));
                return new AjaxResult(HttpStatus.ERROR,"error","请勿频繁请求");
            }else{
                verify.put("insecond",String.valueOf(l));
            }
        }else{
            verify.put("insecond",String.valueOf(l));
        }
        return costHandleService.getFundplanInfo(req, request);
    }

    /**
     * 币种类型信息
     * @param req
     * @param request
     * @return
     */
    @PostMapping("/getCurrtypeInfo")
    public AjaxResult getCurrtypeInfo(@RequestBody String req, HttpServletRequest request){
        String getStaffInfo = verify.get("insecond");
        long l = System.currentTimeMillis();
        if(StringUtils.isNotEmpty(getStaffInfo)){
            Long aLong = Long.valueOf(getStaffInfo);
            long result=l-aLong;
            if(result < ins){
                verify.put("insecond",String.valueOf(l));
                return new AjaxResult(HttpStatus.ERROR,"error","请勿频繁请求");
            }else{
                verify.put("insecond",String.valueOf(l));
            }
        }else{
            verify.put("insecond",String.valueOf(l));
        }
        return costHandleService.getCurrtypeInfo(req, request);
    }

    /**
     * 增值税税码税率 此接口有变动,根据汇率方案查询组织
     * @param req
     * @param request
     * @return
     */
    @PostMapping("/getTaxcodeInfo")
    public AjaxResult getTaxcodeInfo(@RequestBody String req, HttpServletRequest request){
        String getStaffInfo = verify.get("insecond");
        long l = System.currentTimeMillis();
        if(StringUtils.isNotEmpty(getStaffInfo)){
            Long aLong = Long.valueOf(getStaffInfo);
            long result=l-aLong;
            if(result < ins){
                verify.put("insecond",String.valueOf(l));
                return new AjaxResult(HttpStatus.ERROR,"error","请勿频繁请求");
            }else{
                verify.put("insecond",String.valueOf(l));
            }
        }else{
            verify.put("insecond",String.valueOf(l));
        }
        return costHandleService.getTaxcodeInfo(req, request);
    }

    /**
     * 日汇率
     * @param req
     * @param request
     * @return
     */
    @PostMapping("/getCurrrateInfo")
    public AjaxResult getCurrrateInfo(@RequestBody String req, HttpServletRequest request){
        String getStaffInfo = verify.get("insecond");
        long l = System.currentTimeMillis();
        if(StringUtils.isNotEmpty(getStaffInfo)){
            Long aLong = Long.valueOf(getStaffInfo);
            long result=l-aLong;
            if(result < ins){
                verify.put("insecond",String.valueOf(l));
                return new AjaxResult(HttpStatus.ERROR,"error","请勿频繁请求");
            }else{
                verify.put("insecond",String.valueOf(l));
            }
        }else{
            verify.put("insecond",String.valueOf(l));
        }
        return costHandleService.getCurrrateInfo(req, request);
    }

    /**
     * 客户&供应商银行信息
     * @param req
     * @param request
     * @return
     */
    @PostMapping("/getCustbankInfo")
    public AjaxResult getCustbankInfo(@RequestBody String req, HttpServletRequest request){
        String getStaffInfo = verify.get("insecond");
        long l = System.currentTimeMillis();
        if(StringUtils.isNotEmpty(getStaffInfo)){
            Long aLong = Long.valueOf(getStaffInfo);
            long result=l-aLong;
            if(result < ins){
                verify.put("insecond",String.valueOf(l));
                return new AjaxResult(HttpStatus.ERROR,"error","请勿频繁请求");
            }else{
                verify.put("insecond",String.valueOf(l));
            }
        }else{
            verify.put("insecond",String.valueOf(l));
        }
        return costHandleService.getCustbankInfo(req, request);
    }

    /**
     * 供应商信息
     * @param req
     * @param request
     * @return
     */
    @PostMapping("/getSupplierInfo")
    public AjaxResult getSupplierInfo(@RequestBody String req, HttpServletRequest request){
        String getStaffInfo = verify.get("insecond");
        long l = System.currentTimeMillis();
        if(StringUtils.isNotEmpty(getStaffInfo)){
            Long aLong = Long.valueOf(getStaffInfo);
            long result=l-aLong;
            if(result < ins){
                verify.put("insecond",String.valueOf(l));
                return new AjaxResult(HttpStatus.ERROR,"error","请勿频繁请求");
            }else{
                verify.put("insecond",String.valueOf(l));
            }
        }else{
            verify.put("insecond",String.valueOf(l));
        }
        return costHandleService.getSupplierInfo(req, request);
    }

    /**
     * 组织银行账户信息
     * @param req
     * @param request
     * @return
     */
    @PostMapping("/getOrgBankInfo")
    public AjaxResult getOrgBankInfo(@RequestBody String req, HttpServletRequest request){
        String getStaffInfo = verify.get("insecond");
        long l = System.currentTimeMillis();
        if(StringUtils.isNotEmpty(getStaffInfo)){
            Long aLong = Long.valueOf(getStaffInfo);
            long result=l-aLong;
            if(result < ins){
                verify.put("insecond",String.valueOf(l));
                return new AjaxResult(HttpStatus.ERROR,"error","请勿频繁请求");
            }else{
                verify.put("insecond",String.valueOf(l));
            }
        }else{
            verify.put("insecond",String.valueOf(l));
        }
        return costHandleService.getOrgBankInfo(req, request);
    }

    /**
     * 获取会计科目
     * @param req
     * @param request
     * @return
     */
    @PostMapping("/getAccasoa")
    public AjaxResult getAccasoa(@RequestBody String req, HttpServletRequest request){
        String getStaffInfo = verify.get("insecond");
        long l = System.currentTimeMillis();
        if(StringUtils.isNotEmpty(getStaffInfo)){
            Long aLong = Long.valueOf(getStaffInfo);
            long result=l-aLong;
            if(result < ins){
                verify.put("insecond",String.valueOf(l));
                return new AjaxResult(HttpStatus.ERROR,"error","请勿频繁请求");
            }else{
                verify.put("insecond",String.valueOf(l));
            }
        }else{
            verify.put("insecond",String.valueOf(l));
        }
        return costHandleService.getAccasoa(req, request);
    }


    /**
     * 待办入口
     *
     * @param holder
     * @return
     * @throws Exception
     */
    @PostMapping("/eventPush")
    @ResponseBody
    public String eventCallBackReceiver(@RequestBody EncryptionHolder holder) {
        String returnResult = "success";
        try {
            // 1 给三方系统发送待办
            service.eventDealer(holder);
        } catch (Exception e) {
            returnResult = "error";
            logger.error("BIP待办消息推送失败，异常信息：{}", e.getMessage());
        } finally {
            // 2 处理成功，回复 "success" 告知开放平台，否则开放平台会根据事件订阅中的配置的重试次数进行重试推送
            return returnResult;
        }
    }

    @PostMapping("/huidiao")
    public AjaxResult huidiao(@RequestBody String req, HttpServletRequest request){
        String uuid = IdGen.uuid();
        logger.info(uuid+"回调参数p: "+req);
        System.out.println(req);
        String remoteAddr = request.getRemoteAddr();
        logger.info(uuid+"获取供应商信息请求者地址p: "+remoteAddr);
        logger.info(uuid+"回调参数p: "+req);
        System.out.println(req);
        return costHandleService.huidiao(req,request);
    }

    @GetMapping("/huidiao")
    public AjaxResult gethuidiao(@RequestBody String req, HttpServletRequest request){
        String uuid = IdGen.uuid();
        logger.info(uuid+"回调参数: "+req);
        System.out.println(req);
        String remoteAddr = request.getRemoteAddr();
        logger.info(uuid+"获取供应商信息请求者地址: "+remoteAddr);
        logger.info(uuid+"回调参数: "+req);
        System.out.println(req);
        return costHandleService.huidiao(req,request);
    }

    /** 手动同步日期格式为 yyyy MM
     * 推送预算信息到费控平台
     */
    public void pushBudgetTofk(String year, String month){
//        select
//        orgs.name 组织,
//        orgs.code 组织编码,
//        dept.name 部门,
//        dept.code 部门编码,
//        acco.name 会计科目,
//        acco.code 会计科目编码,
//        cub1.pk_ACCP 年,
//        cub1.pk_accm 月,
//        cub1.value 预算金额
//        from
//        tb_cube_01 cub1
//        left join org_orgs orgs1 on cub1.pk_entity = orgs1.pk_org and orgs1.dr=0
//        left join org_dept dept on orgs1.pk_org = dept.pk_dept and dept.dr=0
//        left join org_orgs orgs on dept.pk_org = orgs.pk_org and orgs.dr=0
//        left join bd_account acco on cub1.pk_measure = acco.pk_ORIGINALACCOUNT and acco.dr=0 and acco.pk_accchart in ('0001Z01000ACCCHART01','1001A110000000005QEV')
//        left join bd_accchart acha on acco.pk_accchart = acha.pk_accchart and acha.dr=0 关联主表
//        left join bd_accasoa acas on acco.pk_account = acas.pk_account and acas.pk_accchart=acco.pk_accchart and acas.dr=0  主表一致
//        where cub1.pk_ACCP ='2024' /*期间*/
//        AND cub1.pk_accm = '10'/*月*/
//        AND cub1.code_mvtype ='Budget'  /*业务方案预算数*/
//        AND cub1.pk_entity ='1001A9100000003OU4Z1'/*组织*/
//        AND cub1.code_ys04 = '05'/*业务类型研发费用*/
//        AND cub1.code_ys03 = '03'/*目标类型费用金额*/
//        AND cub1.pk_version = 'TBPTZ410000000001GH1'/*版本*/
//        AND cub1.code_measure = '53010101'  /*指标 会计科目*/
//        and cub1.status2='32'/*审批状态*/


//        select
//        orgs.name pk_orgname,
//        orgs.code pk_orgcode,
//        orgs1.name pk_deptname,
//        orgs1.code pk_deptcode,
//        kmb.pk_measurename pk_measurename,
//        kmb.pk_measurecode pk_measurecode,
//        cub1.pk_ACCP year,
//        cub1.pk_accm month,
//        cub1.value money
//        from
//        tb_cube_01 cub1
//        left join org_orgs orgs1 on cub1.pk_entity = orgs1.pk_org and orgs1.dr=0
//        left join org_dept dept on orgs1.pk_org = dept.pk_dept and dept.dr=0
//        left join org_orgs orgs on dept.pk_org = orgs.pk_org and orgs.dr=0
//        left join
//        (select
//        acco.pk_ORIGINALACCOUNT pk_measure,
//        acas.name pk_measurename,
//        acco.code pk_measurecode
//        from
//        bd_account acco
//        left join bd_accchart acha on acco.pk_accchart = acha.pk_accchart and acha.dr=0
//        left join bd_accasoa acas on acco.pk_account = acas.pk_account and acas.pk_accchart=acco.pk_accchart and acas.dr=0
//        where acco.dr=0 and acco.pk_accchart in ('0001Z01000ACCCHART01','1001A110000000005QEV')
//        UNION ALL
//        select
//        busb.pk_obj pk_measure,
//        busb.objname pk_measurename,
//        busb.objcode	 pk_measurecode
//        from
//        tb_budgetsub busb where busb.dr=0) kmb on cub1.pk_measure = kmb.pk_measure
//                where
//                cub1.status2='32'
//        AND cub1.code_mvtype ='Budget'
//        AND cub1.code_ys03 = '03'
//        AND cub1.code_ys04 = '05'
//        AND cub1.pk_version = 'TBPTZ410000000001GH1'
//        AND orgs1.isbusinessunit = 'N' and orgs1.code != '0000' and orgs1.code != 'global' and orgs1.code != '00'
//        AND kmb.pk_measurecode in ('53010101','660202','660205','5301010604','660203','660204','660207','5301010603','5301010206','5301010207','660216','5301010201','5301010202','5301010203',
//                '660221','53010103','53010104','5301010621','yf02','yf03','yf04')-- 人员人工费用, 办公费, 协会会费, 差旅、会议费用, 差旅费,招待费,车辆费,9知识产权的申请、注册、代理费用,10试制产品的检验费,11仪器设备费用,12校验费,14材料,15燃料,16动力费用,17维修费,18折旧费用,19无形资产摊销,21其他费用8中介服务费, 13保险费, 20长期待摊费用摊销


        costHandleService.pushBudgetTofk(year,month);
    }

    /**
     * 预算自动同步 每天晚上21点10分同步
     */
    @Scheduled(cron="0 10 23 * * ?")
    public void pushBudgetTofkbyh(){
        costHandleService.pushBudgetTofk("","");
    }

    /**
     * 日汇率同步,手动同步日期格式为yyyy-MM-dd 自动则为空自动获取
     * @param date
     */
    public void bushRateTofk(String date){
        costHandleService.bushRateTofk(date);
    }

    /**
     * 日汇率自动同步 同步时间每天中午12点
     */
//    @Scheduled(cron="0 0 12 * * ?")
    public void bushRateTofkbyh(){
        costHandleService.bushRateTofk("");
    }

    //查询资金付款单,根据资金付款单查询统一付款结算,根据统一付款结算状态回写

    /**
     * 同步统一付款结算状态到资金付款单
     */
    public void upbillpaystatus(){
        costHandleService.upbillpaystatus();
    }

    /**
     *  erp推送付款合同到费控平台
     */
    @PostMapping("/pushPayCon")
    public void pushPayCon(){
        costHandleService.pushPayCon();
    }



}
